<?php

    /**
    * @author Ivan Kukic
    * @version 2.0
    * @created 28.Sep.2012 13:46:29
    */
    class object_gallery extends MX_Controller 
    {

        private $data;

        function __construct()
        {
            parent::__construct();
            if(!$this->session->userdata('logged_in'))
                redirect(base_url('login'));

            $this->load->library('gallery');
            $this->load->library('object_gallery_lib');
            $this->load->config('pages');
            $this->load->config('gallery');

        }

        // View gallery pictures
        function view($object_id)
        {
            // find gallery id
            $gallery_id = $this->object_gallery_lib->find_gallery_column($object_id,'gallery_ID');
            $this->data['pictures'] =  $this->gallery->view($gallery_id);
            $this->data['page'] =  'Gallery';
            $this->data['title'] =  'Photos';
            $this->data['object_ID'] =  $object_id;
            $this->data['object_gallery_folder'] =  $this->object_gallery_lib->find_gallery_column($object_id,'path');;
            //$this->firephp->log( $this->data['pictures']);
            $this->template->build('object_gallery/gallery',$this->data);
        }

        // Add pictures to DB and create thumbnails
        function submit_photos()
        {
            // find gallery id
            $gallery_id = $this->object_gallery_lib->find_gallery_column($_POST['object_ID'],'gallery_ID');

            // find pictures and titles
            $pictures =  $_POST['pictures'];
            $titles =  $_POST['pic_titles'];

            // find gallery folder
            $folder = $this->object_gallery_lib->find_gallery_column($_POST['object_ID'],'path');

            // find thumbnail sizes from db
            $this->load->model('settings_model');
            $thumbnails_sizes = $this->settings_model->list_thumbnails(TRUE);

            // create thumbnails
            $this->load->library('pictures');
            $this->pictures->create_thumbnails($folder,$pictures,$thumbnails_sizes);

            // Add pictures to DB
            $this->pictures->create_records($pictures,$titles,$gallery_id);

            // Reload page to preview pictures
            redirect(base_url('object_gallery/view/'.$_POST['object_ID'].'?post_type=realestate'));
        }

        function delete_picture($picture_id,$object_id)
        {

            // get gallery folder
            $folder = $this->object_gallery_lib->find_gallery_column($object_id,'path');

            // get picture filename
            $this->load->library('pictures');
            $filename = $this->pictures->find_picture_column($picture_id,'filename');

            // get thumbnail sizes from db
            $this->load->model('settings_model');
            $thumbnails_sizes = $this->settings_model->list_thumbnails(TRUE);

            // delete picture from pictures table
            $this->pictures->delete($picture_id);

            // delete picture from root gallery folder
            $this->pictures->delete_on_server($folder,$filename,$thumbnails_sizes);

            // Reload page to preview pictures
            redirect(base_url('object_gallery/view/'.$object_id.'?post_type=realestate'));
        }

        // Reorder photos
        function order_photos()
        {
            // find gallery id
            $gallery_id = $this->object_gallery_lib->find_gallery_column($_POST['object_ID'],'gallery_ID');
            $pictures =  $this->gallery->view($gallery_id);

            //order pictures
            $this->load->library('pictures');
            $this->pictures->order($pictures,$_POST['picture-order']);

            // Reload page to preview pictures
            redirect(base_url('object_gallery/view/'.$_POST['object_ID'].'?post_type=realestate'));

        }

        // Create realestate thumbnails again
        function regenerate_thumbnails()
        {
            $this->load->helper('folder');

            $this->load->model('objects_gallery_model');
            $obj_galleries = $this->objects_gallery_model->find_all_objects_galleries();

            // get thumbnail sizes
            $this->load->model("settings_model");

            // default thumbnails
            $this->settings_model->default_thumbnails();
            $thumbnails = $this->settings_model->list_thumbnails();
            $thumbnails_sizes = $this->settings_model->list_thumbnails(TRUE);

            $this->load->library('pictures');

            // go troug each gallery
            foreach ($obj_galleries as $obj_gallery):

                // delete directories
                $this->object_gallery_lib->delete_thumbnails_dir($obj_gallery['objects_ID'],$thumbnails);

                // create directories
                $this->gallery->create_thumbnails_directories($obj_gallery['objects_ID']);

                // find gallery folder
                $folder = $this->object_gallery_lib->find_gallery_column($obj_gallery['objects_ID'],'path');

                // find pictures
                $pictures =  $this->gallery->view_list($obj_gallery['gallery_ID']);

                // create thumbnails
                $this->pictures->create_thumbnails($folder,$pictures,$thumbnails_sizes);

                endforeach;

            echo 'Successful action.';
        }
    }
?>